<template>
  <div class="min-h-screen bg-base-200 flex">
    <!-- 左侧边栏 -->
    <div id="sidebar" class="sidebar sidebar-expanded bg-base-100 shadow-xl border-r border-base-300 flex flex-col transition-all duration-400">
      <!-- 应用标题 -->
      <div class="p-6 border-b border-base-300">
        <div class="flex items-center justify-between">
          <div class="flex items-center space-x-3 fade-text">
            <div class="w-10 h-10 rounded-xl bg-gradient-to-br from-indigo-500 to-purple-600 flex items-center justify-center shadow-lg">
              <i class="fas fa-brain text-white text-lg"></i>
            </div>
            <div>
              <h1 class="font-bold text-xl bg-gradient-to-r from-indigo-600 to-purple-600 bg-clip-text text-transparent">博沃智慧</h1>
              <p class="text-xs text-base-content/70">智能问答</p>
            </div>
          </div>
          <button id="sidebarToggle" class="btn btn-ghost btn-sm">
            <i class="fas fa-bars text-base-content"></i>
            <i class="fas fa-chevron-left text-base-content hidden"></i>
          </button>
        </div>
      </div>

      <!-- 导航菜单 -->
      <div class="flex-1 overflow-y-auto custom-scrollbar p-4">
        <nav class="space-y-2">
          <router-link 
            to="/" 
            class="flex items-center space-x-3 p-3 rounded-lg bg-primary/10 text-primary border border-primary/20 fade-text"
          >
            <i class="fas fa-comments"></i>
            <span class="font-medium">对话界面</span>
          </router-link>
          <router-link to="/settings" class="flex items-center space-x-3 p-3 rounded-lg hover:bg-base-200 transition-colors fade-text">
            <i class="fas fa-cog text-base-content/70"></i>
            <span class="text-base-content">系统设置</span>
          </router-link>
          <router-link to="/knowledge" class="flex items-center space-x-3 p-3 rounded-lg hover:bg-base-200 transition-colors fade-text">
            <i class="fas fa-book text-base-content/70"></i>
            <span class="text-base-content">知识库管理</span>
          </router-link>
          <router-link to="/mcp-management" class="flex items-center space-x-3 p-3 rounded-lg hover:bg-base-200 transition-colors fade-text">
            <i class="fas fa-plug text-base-content/70"></i>
            <span class="text-base-content">MCP管理</span>
          </router-link>
        </nav>
      </div>

      <!-- 用户信息 -->
      <div class="p-4 border-t border-base-300">
        <div class="flex items-center space-x-3">
          <div class="avatar">
            <div class="w-12 h-12 rounded-full bg-gradient-to-br from-emerald-400 to-cyan-500 flex items-center justify-center shadow-lg">
              <span class="text-white text-lg font-bold">{{ authStore.username?.charAt(0).toUpperCase() }}</span>
            </div>
          </div>
          <div class="fade-text flex-1">
            <p class="font-semibold text-base-content">{{ authStore.username }}</p>
            <p class="text-xs text-base-content/70">专业版用户</p>
          </div>
          <div class="fade-text">
            <button @click="logout" class="btn btn-ghost btn-sm">
              <i class="fas fa-sign-out-alt text-base-content/70"></i>
            </button>
          </div>
        </div>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="flex-1 flex flex-col bg-base-100">
      <!-- 顶部标题栏 -->
      <div class="bg-base-100 border-b border-base-300 p-6">
        <div class="flex items-center justify-between">
          <div>
            <h1 class="text-2xl font-bold text-base-content">智能对话</h1>
            <p class="text-base-content/70 mt-1">与AI助手进行智能问答</p>
          </div>
          <div class="flex items-center space-x-4">
            <button class="btn btn-ghost btn-sm">
              <i class="fas fa-history mr-2"></i>对话历史
            </button>
            <button class="btn btn-ghost btn-sm">
              <i class="fas fa-cog mr-2"></i>设置
            </button>
          </div>
        </div>
      </div>

      <!-- 统计卡片 -->
      <div class="bg-base-100 border-b border-base-300 p-6">
        <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
          <div class="stat bg-gradient-to-br from-blue-500 to-blue-600 text-white">
            <div class="stat-figure">
              <i class="fas fa-comments text-3xl"></i>
            </div>
            <div class="stat-title text-blue-100">总对话数</div>
            <div class="stat-value text-2xl">1,234</div>
            <div class="stat-desc text-blue-100">↗︎ 12.5%</div>
          </div>
          
          <div class="stat bg-gradient-to-br from-green-500 to-green-600 text-white">
            <div class="stat-figure">
              <i class="fas fa-calendar-check text-3xl"></i>
            </div>
            <div class="stat-title text-green-100">本月活跃</div>
            <div class="stat-value text-2xl">89</div>
            <div class="stat-desc text-green-100">↗︎ 8.3%</div>
          </div>
          
          <div class="stat bg-gradient-to-br from-yellow-500 to-yellow-600 text-white">
            <div class="stat-figure">
              <i class="fas fa-coins text-3xl"></i>
            </div>
            <div class="stat-title text-yellow-100">Token使用</div>
            <div class="stat-value text-2xl">45.6K</div>
            <div class="stat-desc text-yellow-100">↗︎ 23.1%</div>
          </div>
          
          <div class="stat bg-gradient-to-br from-purple-500 to-purple-600 text-white">
            <div class="stat-figure">
              <i class="fas fa-thumbs-up text-3xl"></i>
            </div>
            <div class="stat-title text-purple-100">满意度</div>
            <div class="stat-value text-2xl">94.2%</div>
            <div class="stat-desc text-purple-100">↗︎ 2.1%</div>
          </div>
        </div>
      </div>

      <!-- 对话区域 -->
      <div class="flex-1 flex">
        <!-- 对话列表 -->
        <div class="w-80 bg-base-200 border-r border-base-300 flex flex-col">
          <div class="p-4 border-b border-base-300">
            <button class="btn btn-primary btn-block">
              <i class="fas fa-plus mr-2"></i>新建对话
            </button>
          </div>
          
          <div class="flex-1 overflow-y-auto custom-scrollbar p-4 space-y-2">
            <!-- 对话项 -->
            <div class="card bg-base-100 shadow-sm cursor-pointer hover:shadow-md transition-shadow">
              <div class="card-body p-4">
                <h3 class="font-semibold text-base-content mb-1">数据分析咨询</h3>
                <p class="text-sm text-base-content/70 mb-2">如何分析用户行为数据？</p>
                <div class="flex justify-between items-center">
                  <span class="text-xs text-base-content/50">今天 14:30</span>
                  <span class="badge badge-primary">进行中</span>
                </div>
              </div>
            </div>
            
            <div class="card bg-base-100 shadow-sm cursor-pointer hover:shadow-md transition-shadow">
              <div class="card-body p-4">
                <h3 class="font-semibold text-base-content mb-1">技术问题解决</h3>
                <p class="text-sm text-base-content/70 mb-2">Vue 3 组合式API最佳实践</p>
                <div class="flex justify-between items-center">
                  <span class="text-xs text-base-content/50">昨天 16:45</span>
                  <span class="badge badge-success">已完成</span>
                </div>
              </div>
            </div>
            
            <div class="card bg-base-100 shadow-sm cursor-pointer hover:shadow-md transition-shadow">
              <div class="card-body p-4">
                <h3 class="font-semibold text-base-content mb-1">项目规划</h3>
                <p class="text-sm text-base-content/70 mb-2">如何制定有效的项目计划？</p>
                <div class="flex justify-between items-center">
                  <span class="text-xs text-base-content/50">2天前</span>
                  <span class="badge badge-success">已完成</span>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- 聊天界面 -->
        <div class="flex-1 flex flex-col">
          <!-- 聊天消息区域 -->
          <div class="flex-1 overflow-y-auto custom-scrollbar p-6 space-y-4">
            <!-- AI消息 -->
            <div class="flex items-start space-x-3">
              <div class="avatar">
                <div class="w-10 h-10 rounded-full bg-gradient-to-br from-indigo-500 to-purple-600 flex items-center justify-center">
                  <i class="fas fa-robot text-white"></i>
                </div>
              </div>
              <div class="max-w-2xl">
                <div class="card bg-base-100 shadow-sm">
                  <div class="card-body p-4">
                    <p class="text-base-content">您好！我是您的AI助手。我可以帮助您解答问题、提供信息、协助您完成各种任务。请告诉我您需要什么帮助？</p>
                  </div>
                </div>
                <div class="text-xs text-base-content/50 mt-1">今天 15:20</div>
              </div>
            </div>

            <!-- 用户消息 -->
            <div class="flex items-start space-x-3 justify-end">
              <div class="max-w-2xl">
                <div class="card bg-primary text-primary-content shadow-sm">
                  <div class="card-body p-4">
                    <p>我想了解如何使用Vue 3和TypeScript构建现代化的Web应用。</p>
                  </div>
                </div>
                <div class="text-xs text-base-content/50 mt-1 text-right">今天 15:21</div>
              </div>
              <div class="avatar">
                <div class="w-10 h-10 rounded-full bg-gradient-to-br from-emerald-400 to-cyan-500 flex items-center justify-center">
                  <span class="text-white font-bold">{{ authStore.username?.charAt(0).toUpperCase() }}</span>
                </div>
              </div>
            </div>

            <!-- AI回复 -->
            <div class="flex items-start space-x-3">
              <div class="avatar">
                <div class="w-10 h-10 rounded-full bg-gradient-to-br from-indigo-500 to-purple-600 flex items-center justify-center">
                  <i class="fas fa-robot text-white"></i>
                </div>
              </div>
              <div class="max-w-2xl">
                <div class="card bg-base-100 shadow-sm">
                  <div class="card-body p-4">
                    <p class="text-base-content mb-3">使用Vue 3和TypeScript构建现代化Web应用是一个很好的选择！以下是一些关键步骤和最佳实践：</p>
                    
                    <div class="space-y-2">
                      <div>
                        <h4 class="font-semibold text-base-content">1. 项目初始化</h4>
                        <p class="text-sm text-base-content/70">使用Vite创建项目，它提供了更好的开发体验和构建性能。</p>
                      </div>
                      
                      <div>
                        <h4 class="font-semibold text-base-content">2. TypeScript配置</h4>
                        <p class="text-sm text-base-content/70">确保tsconfig.json配置正确，启用严格模式以获得更好的类型安全。</p>
                      </div>
                      
                      <div>
                        <h4 class="font-semibold text-base-content">3. 组合式API</h4>
                        <p class="text-sm text-base-content/70">使用Vue 3的组合式API，它提供了更好的逻辑复用和类型推断。</p>
                      </div>
                    </div>
                  </div>
                </div>
                <div class="text-xs text-base-content/50 mt-1">今天 15:22</div>
              </div>
            </div>
          </div>

          <!-- 输入区域 -->
          <div class="border-t border-base-300 p-4">
            <div class="flex space-x-4">
              <div class="flex-1">
                <div class="join">
                  <input
                    type="text"
                    placeholder="输入您的问题..."
                    class="input input-bordered join-item flex-1"
                  />
                  <button class="btn btn-primary join-item">
                    <i class="fas fa-paper-plane"></i>
                  </button>
                </div>
              </div>
              <div class="dropdown dropdown-end">
                <button class="btn btn-ghost">
                  <i class="fas fa-ellipsis-v"></i>
                </button>
                <ul class="dropdown-content menu p-2 shadow-xl bg-base-100 rounded-box w-52">
                  <li><a>清空对话</a></li>
                  <li><a>导出记录</a></li>
                  <li><a>设置</a></li>
                </ul>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { useAuthStore } from '../stores/auth';

const router = useRouter();
const authStore = useAuthStore();

const sidebarCollapsed = ref(false);

// 侧边栏折叠功能
const toggleSidebar = () => {
  sidebarCollapsed.value = !sidebarCollapsed.value;
  const sidebar = document.getElementById('sidebar');
  const fadeTexts = document.querySelectorAll('.fade-text');
  const barsIcon = document.querySelector('#sidebarToggle .fa-bars');
  const chevronIcon = document.querySelector('#sidebarToggle .fa-chevron-left');
  
  if (sidebarCollapsed.value) {
    sidebar?.classList.add('sidebar-collapsed');
    sidebar?.classList.remove('sidebar-expanded');
    barsIcon?.classList.add('hidden');
    chevronIcon?.classList.remove('hidden');
  } else {
    sidebar?.classList.remove('sidebar-collapsed');
    sidebar?.classList.add('sidebar-expanded');
    barsIcon?.classList.remove('hidden');
    chevronIcon?.classList.add('hidden');
  }
  
  setTimeout(() => {
    fadeTexts.forEach((text: any) => {
      if (sidebarCollapsed.value) {
        text.classList.add('hidden');
      } else {
        text.classList.remove('hidden');
      }
    });
  }, sidebarCollapsed.value ? 0 : 200);
};

// 登出功能
const logout = async () => {
  await authStore.logout();
  router.push('/login');
};

onMounted(() => {
  const sidebarToggle = document.getElementById('sidebarToggle');
  if (sidebarToggle) {
    sidebarToggle.addEventListener('click', toggleSidebar);
  }
});
</script>

<style scoped>
/* 自定义滚动条 */
.custom-scrollbar::-webkit-scrollbar {
  width: 6px;
}

.custom-scrollbar::-webkit-scrollbar-track {
  background: hsl(var(--b2));
  border-radius: 3px;
}

.custom-scrollbar::-webkit-scrollbar-thumb {
  background: hsl(var(--bc) / 0.3);
  border-radius: 3px;
}

.custom-scrollbar::-webkit-scrollbar-thumb:hover {
  background: hsl(var(--bc) / 0.5);
}

/* 侧边栏动画 */
.sidebar {
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.sidebar-collapsed {
  width: 64px !important;
}

.sidebar-expanded {
  width: 15% !important;
}

/* 文本淡入淡出 */
.fade-text {
  transition: opacity 0.3s ease-in-out;
}

.fade-text.hidden {
  opacity: 0;
  pointer-events: none;
}
</style>